package controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.form.FormBeanFactory;

import util.Util;
import bean.Customer;
import bean.Fund;
import bean.FundTransaction;
import bean.LoggedUser;
import bean.Model;
import bean.TransactionStatus;
import bean.TransactionType;
import bean.UserType;
import dao.CustomerDAO;
import dao.FundDAO;
import dao.FundTransactionDAO;
import formBean.BuyFundForm;

/*
 * 
 * Created By Sahil Thakkar for Buy Fund Usecase
 */

public class BuyFundAction extends Action {
	
	private FundTransactionDAO fundTransactionDAO;
	private FundTransaction fundTransaction;
	private FundDAO fundDAO;
	private CustomerDAO customerDAO;
	private FormBeanFactory<BuyFundForm> formBeanFactory = FormBeanFactory.getInstance(BuyFundForm.class);
	
	public BuyFundAction(Model model) {
		fundDAO = model.getFundDAO();
		customerDAO = model.getCustomerDAO();
		this.fundTransactionDAO = model.getFundTransactionDAO();
	}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
	
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "buyFund.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		// TODO Auto-generated method stub
		List<String> errors = new ArrayList<String>();
		List<Fund> fundList = null;
		
		try {
			HttpSession session     = request.getSession(false);
			LoggedUser user = (LoggedUser) session.getAttribute("user");
			long balance2 = customerDAO.getCustomerCash(""+user.getId());
			double dblbalance2 = balance2/100.0;
			BuyFundForm form = formBeanFactory.create(request);
			form.setBalance(""+balance2);
			request.setAttribute("form", form);
			
			long balance1 = customerDAO.getCustomerCash(""+user.getId());
			
			if (!form.isPresent()) {
				return "openBuyFund.jsp";
			}
			
			
			
			
			errors.addAll(form.getValidationErrors());
			if (errors.size() > 0) {
				request.setAttribute("errors", errors);
				long balance = customerDAO.getCustomerCash(""+user.getId());
				
				fundList = fundDAO.getAllFund();

				request.setAttribute("fundList", fundList);
				request.setAttribute("balance", util.Util.showMoney(balance));
				return "openBuyFund.jsp";
			}
			if(balance1 >= Util.convertToDBMoney(form.getAmount()))
			{
				fundTransaction = new FundTransaction();
				Customer c = new Customer();
				c.setId(user.getId());
				fundTransaction.setCustomer(c);

				Fund f = fundDAO.getFund(Integer.parseInt(form.getFundList()));
				fundTransaction.setFund(f);
				fundTransaction.setTransactionType(TransactionType.BUY);
				fundTransaction.setAmount(Util.convertToDBMoney(form.getAmount()));
				fundTransaction.setOrderDate(new Date());
				fundTransaction.setStatus(TransactionStatus.Pending);
				fundTransactionDAO.createFundTransaction(fundTransaction);

				request.setAttribute("message", "You have bought the Fund " + f.getName() + ".");
				return "employeeConsole.jsp";
			}
			else
			{
				errors.add("You do not have sufficient balance");
				if (errors.size() > 0) {
					request.setAttribute("errors", errors);
					long balance = customerDAO.getCustomerCash(""+user.getId());
					
					fundList = fundDAO.getAllFund();

					request.setAttribute("fundList", fundList);
					request.setAttribute("balance", util.Util.showMoney(balance));
					return "openBuyFund.jsp";
				}
			}
			return "error.jsp";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error.jsp";
		}
		
	}

	@Override
	public UserType getType() {
		// TODO Auto-generated method stub
		return UserType.Customer;
	}
}
